package nl.ajonker.railroad.dl;

import java.util.LinkedList;

public class Track {
	private final LinkedList<Rail> rails = new LinkedList<>();
	private final LinkedList<Train> trains = new LinkedList<>();

	private Rail first;
	private Rail last;

	public void addRail(Rail rail) {
		if (first == null) {
			first = rail;
		} else if (rail != last) {
			Rail.connect(last, rail);
		}
		rails.add(rail);
		last = rail;

	}

	public void finish() {
		Rail.connect(last, first);
	}

	public void addTrain(Train train) {
		trains.add(train);
	}

	public void step() {
		for (Train train : trains)
			train.move();
	}

	public LinkedList<Rail> getRails() {
		return rails;
	}
}
